/*
 * @Author: wurangkun
 * @Date: 2024-05-16 09:05:26
 * @LastEditTime: 2024-05-22 17:05:14
 * @LastEditors: wurangkun
 * @FilePath: \flutter_example\lib\routes\routes.dart
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:flutter_example/common/UserModel.dart';
import 'package:flutter_example/home/home.dart';
import 'package:flutter_example/tabbar.dart';
import 'package:flutter_example/widgets/AnimatedList.dart';
import 'package:flutter_example/login/login.dart';

Map route = {
  '/': const TabbarPage(),
  'animatedList': const AnimatedListPage(),
  'login': const LoginPage()
  // Add more routes here
};

// var onGenerateRoute = (RouteSettings settings) {
//   return MaterialPageRoute(builder: (context) => route[settings.name]!);
// };

var onGenerateRoute = (RouteSettings settings) {
  UserModel user = UserModel();
  // Handle '/'
  print(user.isLogin);
  print(settings.name);
  if (user.isLogin) {
    if (route.isNotEmpty) {
      if (settings.name == '/') {
        return MaterialPageRoute(builder: (context) => route[settings.name]!);
      }
      return createRoute(route[settings.name]);
    }
  } else {
    return MaterialPageRoute(builder: (context) => const LoginPage());
  }

  return MaterialPageRoute(builder: (context) => const UnknownScreen());
};

class UnknownScreen extends StatelessWidget {
  const UnknownScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

var createRoute = (Widget page) => PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) => page,
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        const begin = Offset(1.0, 0.0);
        const end = Offset.zero;
        const curve = Curves.ease;

        var tween =
            Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

        return SlideTransition(
          position: animation.drive(tween),
          child: child,
        );
      },
    );
